// pages/person/login/index.js
const app = getApp();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        is_user: {
            // 接收参数的类型
            type: String,
            // 接收参数的默认值
            value: "",
            // 为0则不显示
            observer(e) {
                if (e == 1) {
                    this.setData({
                        loginModalVisible: true,
                        loginWXVisible: true,
                        loginMobileVisible: false,
                    });
                } else if (e == 2) {
                    this.setData({
                        loginModalVisible: true,
                        loginMobileVisible: true,
                        loginWXVisible: false,
                    });
                }
            },
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        loginModalVisible: false,
        loginWXVisible: false,
        loginMobileVisible: false,
        mobile: "",
        password: "",
        userInfo: [],
    },

    /**
     * 组件的方法列表
     */

    methods: {
        close() {
            this.triggerEvent("is_user_show", true);
            this.setData({
                loginModalVisible: false,
                is_user: 0,
            });
        },
        getUserInfoData(token) {
            const that = this
            app.http.post(app.api.userInfo, {
                token
            }, (res) => {
                that.triggerEvent("userInfo", res.data);
                // wx.setStorageSync("userInfo", res.data);
                if (res.data.mobile == "") {
                    that.setData({
                        loginWXVisible: false,
                        loginMobileVisible: true,
                    });
                } else {
                    that.triggerEvent("is_user_show", true);
                    that.setData({
                        loginModalVisible: false,
                        is_user: 0,
                    });
                }

            })
        },
        getUserInfo: function (e) {
            const that = this;
            console.log(e);
            wx.getUserProfile({
                lang: "zh_CN",
                desc: "用于获取您的个人信息", // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
                success: (re) => {
                    console.log("获取用户信息成功", re);
                    wx.login({
                        success: function (res) {
                            app.http.post(app.api.getOpenid, {
                                code: res.code
                            }, (reso) => {
                                app.http.post(app.api.threeLogin, {
                                    Sharestoreid: wx.getStorageSync('Sharestoreid'),
                                    Shareuid: wx.getStorageSync('Shareuid'),
                                    openid: reso.data.openid,
                                    nickName: re.userInfo.nickName,
                                    gender: re.userInfo.gender,
                                    avatarUrl: re.userInfo.avatarUrl
                                }, (res) => {
                                    wx.setStorageSync("uid", res.data.uid);
                                    wx.setStorageSync("token", res.data.token);
                                    wx.setStorageSync("openid", reso.data.openid);
                                    that.getUserInfoData(res.data.token)
                                })
                            })


                        },
                    });
                },
                fail: (re) => {
                    console.log("获取用户信息失败", re);
                },
            });
        },
        getPhoneNumber: function (e) {
            console.log(e)
            const that = this;
            wx.login({
                success: function (res) {
                    if (e.detail.errMsg == "getPhoneNumber:ok") {
                        app.http.post(
                            app.api.userMobile, {
                                code: res.code,
                                encryptedData: e.detail.encryptedData,
                                iv: e.detail.iv,
                            }, (resm) => {
                                app.http.post(
                                    app.api.bindingModel, {
                                        mobile: resm.data
                                    }, (res1) => {
                                        wx.setStorageSync("mobile", resm.data);
                                        that.getUserInfoData(wx.getStorageSync('token'))
                                        that.triggerEvent("is_user_show", true);
                                    })
                            })
                    } else {
                        return;
                    }
                },
            });
        },
    },
});